package MTRequest;

import MTClient.MTConstant;
import MTClient.MessageCentre;
import SQLManager.SQLManager;

/**
 * This class handle to synchronize the Server's DB with the User's local DB.
 * @author King
 *
 */
public class MTClientSynDataFromServerDB extends MTRequest {
	
	
	public MTClientSynDataFromServerDB( MessageCentre aMSGCenter )
	{
		super( aMSGCenter );
		this.mRequestState = MTConstant.MT_REQ_CSYN_DFSDB_UPDATE_REQUEST;
	}
	
	protected void UpdateRequestState( int aPeerState )
	{
		if( MTConstant.MT_REQ_SSYN_DFSDB_UPDATED_PRODUCT == aPeerState ){
			this.mRequestState = MTConstant.MT_REQ_CSYN_DFSDB_UPDATING_DB;
		}
		else if( MTConstant.MT_REQ_SSYN_DFSDB_UPDATED_PRODUCT_LAST == aPeerState ){
			this.mRequestState = MTConstant.MT_REQ_CSYN_DFSDB_UPDATING_DB_LAST;
		}
		else if( MTConstant.MT_REQ_SSYN_DFSDB_NO_UPDATED == aPeerState ){
			this.mRequestState = MTConstant.MT_REQ_FINISH;
		}
	}
	
	protected void ProcessReqest( int aPeerState, String peerRawData )
	{
		switch( aPeerState ){
			case MTConstant.MT_REQ_SSYN_DFSDB_NO_UPDATED:
				this.mRequestDataSource.Notify(MTConstant.MT_REQID_SYNCHRONIZE_DATA_FROM_SERVER,aPeerState, null);
				break;
			case MTConstant.MT_REQ_SSYN_DFSDB_UPDATED_PRODUCT:
			case MTConstant.MT_REQ_SSYN_DFSDB_UPDATED_PRODUCT_LAST:{
				this.mReceivedFromPeerRawData.add( peerRawData );
				
				String [] rawData = peerRawData.split("" + MTConstant.MT_REQ_RAW_DATA_SEPARATER);
				//
				// Update Local DB
				//
				switch( Integer.parseInt( rawData[0] )){
					case MTConstant.MT_ID_UPT_CREATE_PRODUCT:{
						HandleCreateProduct( rawData );
						break;
					}
					case MTConstant.MT_ID_UPT_CREATE_TABLE:{
						HandleCreateEventTable( rawData );
						break;
					}
					case MTConstant.MT_ID_UPT_STOCK_TAKE_EVENT:{
						HandleCreateDebtTable( rawData );
						break;
					}
				}
				
				this.mRequestDataSource.Notify(MTConstant.MT_REQID_SYNCHRONIZE_DATA_FROM_SERVER,aPeerState, null);
				
				break;
			}
		}
	}
	
	public void SendRequest()
	{
		String request = "" + MTConstant.MT_REQID_SYNCHRONIZE_DATA_FROM_SERVER + MTConstant.MT_REQ_ITEM_SEPARATER + 
		this.mRequestState + MTConstant.MT_REQ_ITEM_SEPARATER;
		switch( this.mRequestState ){
			case MTConstant.MT_REQ_CSYN_DFSDB_UPDATE_REQUEST:{
				String userID = this.mRequestDataSource.GetRawData();
				request += userID;
				break;
			}
			case MTConstant.MT_REQ_CSYN_DFSDB_UPDATING_DB:
			case MTConstant.MT_REQ_CSYN_DFSDB_UPDATING_DB_LAST:{
				request += " ";
				break;
			}
			case MTConstant.MT_REQ_FINISH:{
				this.Finish();
				return;
			}
		}
		this.SendMSG(request);
		
		if( MTConstant.MT_REQ_CSYN_DFSDB_UPDATING_DB_LAST == this.mRequestState ){
			this.Finish();
		}
	}
	
	
	private void HandleCreateEventTable( String [] aEventInfo )
	{
		// Handle Create Event
		SQLManager sqlManager = SQLManager.GetInstance();
		sqlManager.open();
		sqlManager.InsertEvent( aEventInfo[1], aEventInfo[2], aEventInfo[3], aEventInfo[4], aEventInfo[5]);
		
		String tempEventID = sqlManager.GetLastIndex( SQLManager.TB_EVENTTABLE );
		sqlManager.CreateProductList( tempEventID );
		sqlManager.close();
	}
	
	private void HandleCreateProduct( String [] aProductInfo )
	{
		// Handle Create Product
		SQLManager sqlManager = SQLManager.GetInstance();
		sqlManager.open();
		String eventID = aProductInfo[1];
		sqlManager.InsertProductWithEventID(eventID, aProductInfo[2], aProductInfo[3], aProductInfo[4], aProductInfo[5], aProductInfo[6], aProductInfo[7], aProductInfo[8] );
		sqlManager.close();
	}
	
	private void HandleCreateDebtTable( String [] aDebtTableData )
	{
		for( int i = 0 ; i < aDebtTableData.length ; ++i ){
			System.out.println( "" + i + ":" + aDebtTableData[i] );
		}
		
		// Map EventID
		SQLManager sqlManager = SQLManager.GetInstance();
		sqlManager.open();
		
		String tempEventID = sqlManager.GetMappedTempEventID( aDebtTableData[1] );
		// Create Debt Table
		sqlManager.CreateDebtTable( tempEventID );
		
		// Insert Debt Item
		int curIndex = 2;
		
		while( curIndex < aDebtTableData.length ){
			sqlManager.InsertDebtItem( 
					tempEventID , 
					aDebtTableData[curIndex], 
					aDebtTableData[curIndex+1], 
					aDebtTableData[curIndex+2]);
			curIndex += 3;
		}
		
		// Terminated Event
		String query = String.format(
				"UPDATE EventTable SET IsTerminated = 1 WHERE ID = %s"
				, tempEventID );
		SQLManager.GetInstance().getWritableDatabase().execSQL( query );
		
		sqlManager.close();
	}

}
